package mi.payment.ui.print;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.ui.print.helper.TotalSettlementDetail;
import mi.payment.controller.BaseTotalSettlementDetailController;
import mi.payment.controller.TotalSettlementDetailController;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.ui.base.StateView;
import mi.payment.ui.base.StateView.StateType;
import mi.payment.ui.print.helper.BasePrintController;
import mi.payment.ui.print.helper.PrintController;
import mi.payment.utils.print.DataPrinterKernel;
import sunmi.sunmiui.title.Title;

/**
 * 结算总计页面
 */
public class TotalSettlementDetailActivity extends AutoFinishActivity {

    private static final String EXTRA_LAST_BATCH = "extra_last_batch";

    private Title tile;
    private TextView merchantName;
    private TextView merchantNumber;
    private TextView merchantTerminal;
    private TextView batchNumber;
    private TextView date;
    private TextView time;

    private LinearLayout detailView;

    private View contentLayout;
    private StateView noTransaction;

    private TotalSettlementDetail tsDetail;
    private BasePrintController printController;

    private BaseTotalSettlementDetailController totalSettlementDetailController;

    private boolean lastBatch;

    public static void start(Context context, boolean lastBatch) {
        Intent starter = new Intent(context, TotalSettlementDetailActivity.class);
        starter.putExtra(EXTRA_LAST_BATCH, lastBatch);
        context.startActivity(starter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_total_settlement_detail_activity);
        lastBatch = getIntent().getBooleanExtra(EXTRA_LAST_BATCH, false);
        initView();
        initData();
        setScreenData();
    }

    private void initView() {
        tile = (Title) findViewById(R.id.title);
        if (lastBatch) {
            tile.setTitleText(getString(R.string.print_last_batch_settlement_summary));
        } else {
            tile.setTitleText(getString(R.string.tran_make_the_settlement_summary));
        }
        tile.setHeadBgColor(Color.parseColor("#FD5A51"));
        tile.setImageViewAddDrawable(R.drawable.printer);
        tile.setButtonVisibility(false, false, true, true);
        try {
            tile.setMenuClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            }, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        int printStatus = DataPrinterKernel.INSTANCE().getPrinterService().updatePrinterState();
                        if (printStatus == 1) {
                            tile.setButtonVisibility(false, true, true, true);
                            print();
                        } else {
                            showToast(R.string.printer_error);
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        showToast(R.string.printer_error);
                    }
                }
            }, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        noTransaction = (StateView) findViewById(R.id.no_transaction);
        noTransaction.setStateType(0, StateType.NO_ORDER);
        contentLayout = findViewById(R.id.content_layout);
        merchantName = (TextView) findViewById(R.id.merchant_name);
        merchantNumber = (TextView) findViewById(R.id.merchant_number);
        merchantTerminal = (TextView) findViewById(R.id.merchant_terminal);
        batchNumber = (TextView) findViewById(R.id.batch_number);
        date = (TextView) findViewById(R.id.date);
        time = (TextView) findViewById(R.id.time);

        detailView = (LinearLayout) findViewById(R.id.view_detail);
    }

    private void initData() {
        printController = new PrintController(this, lastBatch);
        printController.initPrintData();
        tsDetail = printController.getTotalSettlementDetail();
        totalSettlementDetailController = new TotalSettlementDetailController(this);
    }

    @SuppressLint("SetTextI18n")
    private void setScreenData() {
        if (tsDetail == null) {
            tile.setButtonVisibility(false, true, true, true);
            noTransaction.setVisibility(View.VISIBLE);
            contentLayout.setVisibility(View.GONE);
            return;
        }
        merchantName.setText(getString(R.string.receipt_3_e) + nullToString(tsDetail.shopName));
        merchantNumber.setText(getString(R.string.receipt_4_e) + nullToString(tsDetail.shopNumber));
        merchantTerminal.setText(getString(R.string.receipt_5_e) + nullToString(tsDetail.terminalNumber));
        batchNumber.setText(getString(R.string.receipt_10_e) + nullToString(tsDetail.batchNumber));
        date.setText(getString(R.string.receipt_12_e) + nullToString(tsDetail.date));
        time.setText(getString(R.string.receipt_13_e) + nullToString(tsDetail.time));

        View settlementDetailView = totalSettlementDetailController.getSettlementDetailView(tsDetail);
        detailView.addView(settlementDetailView);
    }

    private void print() {
        try {
            printController.printTotalSettlementDetail(null);
        } catch (Exception e) {
            e.printStackTrace();
            showToast(R.string.error_printer);
        }
    }

    private String nullToString(String src) {
        return src == null ? "" : src;
    }


}
